#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
Simplified BSD License (BSD)
Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this 
      list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/
#endregion

using System;
using System.Drawing;

namespace Thn.Drawing
{
    public class HslColor
    {
        #region Lighten
        /// <summary>
        /// Lightens the colour by the specified amount by modifying
        /// the luminance (for example, 0.2 would lighten the colour by 20%)
        /// </summary>
        public void Lighten(float percent)
        {
            mLuminance *= (1.0f + percent);
            if (mLuminance > 1.0f)
            {
                mLuminance = 1.0f;
            }
        }
        #endregion

        #region Darken
        /// <summary>
        /// Darkens the colour by the specified amount by modifying
        /// the luminance (for example, 0.2 would darken the colour by 20%)
        /// </summary>
        public void Darken(float percent)
        {
            mLuminance *= (1.0f - percent);
        }
        #endregion

        #region Properties
        float mHue;
        float mSaturation = 1f;
        float mLuminance = 0.5f;

        private byte mAlpha = 255;
        /// <summary>
        /// Gets/Sets the alpha property (actually not in HSL color space)
        /// </summary>
        public byte Alpha
        {
            get { return mAlpha; }
            set { mAlpha = value; }
        }

        /// <summary>
        /// Gets/Sets the hue property in range [0, 360]
        /// </summary>
        public float Hue
        {
            get { return mHue; }
            set
            {
                if (value < 0.0f) mHue = 360f + value;
                else if (value > 360f) mHue = 360f - value;
                else mHue = value;
            }
        }

        /// <summary>
        /// Gets/Sets the saturation property in range [0, 1]
        /// </summary>
        public float Saturation
        {
            get { return mSaturation; }
            set
            {
                mSaturation = value;
                mSaturation = mSaturation > 1 ? 1 : mSaturation < 0 ? 0 : mSaturation;
            }
        }

        /// <summary>
        /// Gets/Sets the luminance property in range [0, 1]
        /// </summary>
        public float Luminance
        {
            get { return mLuminance; }
            set
            {
                mLuminance = value;
                mLuminance = mLuminance > 1 ? 1 : mLuminance < 0 ? 0 : mLuminance;
            }
        }

        #endregion

        #region Constructor
        public HslColor()
        {
            mHue = 0;
            mSaturation = 1f;
            mLuminance = 0.5f;
        }

        public HslColor(float Hue, float Saturation, float Luminance)
        {
            mHue = Hue;
            mSaturation = Saturation;
            mLuminance = Luminance;
        }
        #endregion

        #region Casting
        #region HSL <-> Argb
        public static implicit operator Color(HslColor hsl)
        {
            return ColorConverter.HSLmtomArgb(hsl);
        }

        public static implicit operator HslColor(Color Argb)
        {
            return ColorConverter.ArgbmtomHSL(Argb);
        }
        #endregion

        #region HSL -> CYMK
        public static implicit operator CymkColor(HslColor hsl)
        {
            Color tmp = ColorConverter.HSLmtomArgb(hsl);
            return ColorConverter.ArgbmtomCYMK(tmp);
        }
        #endregion
        #endregion

        #region ToString
        public override string ToString()
        {
            return string.Format("H: {0:#} S: {1:#.##} L: {2:#.##}", mHue, mSaturation, mLuminance);
        }
        #endregion
    }
}
